// Wanktube.js for PSPTube
// Made By JK108

function Wanktube_CheckURL( url, option )
{
	if (url.match("http://www.wanktube.com/") == "http://www.wanktube.com/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Wanktube URL
*/
function Wanktube_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/\?flv\=(.*?)\"/);
	var video_url = video_url[1];
	return video_url;
}

function Wanktube_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 15;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.wanktube.com/search_result.php?search_id=" + keyword.replace(/ /g, "+") + "&search_type=search_videos" );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.wanktube.com/search_result.php?search_id=" + keyword.replace(/ /g, "+") + "&search_type=search_videos&page=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	result.total = 10000;


	while(1){
	var pos = contents.indexOf("<DIV class=moduleEntry>");

	if(pos != -1){
		contents = contents.substring(pos + 23, contents.length);
		//Get URL
		var url = contents.match(/\<A href\=\"(.*?)\"/);
		var url = url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<IMG class\=moduleEntryThumb height\=75 src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		pos = contents.indexOf("<DIV class=moduleEntryTitle>");
		var title = contents.substring(pos, contents.length);
		pos = title.indexOf("\">");
		title = title.substring(pos + 2, title.length);
		pos = title.indexOf("</A></DIV>");
		title = ImproveText(title.substring(0, pos));
		//Length
		var length = contents.match(/\sRuntime\: (.*?) \|/);
		pos = length[1].indexOf(".");
		if(pos != -1){ var length = parseInt(length[1].substring(0, pos)); }
		else{ var length = 0; }
		//Description
		pos = contents.indexOf("<DIV class=moduleEntryDescription>");
		var description = contents.substring(pos + 34, contents.length);
		pos = description.indexOf("</DIV>");
		description = ImproveText(description.substring(0, pos));
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 60);
		//Views
		var views = contents.match(/\| Views\: (.*?) \|/);
		var views = parseInt(views[1]);
		//Comments
		var comments = contents.match(/\sComments\: (.*?)\n/);
		var comments = parseInt(comments[1]);
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Wanktube = new Object();
Wanktube.Name          = "Wanktube";		/* Service name */
Wanktube.Description   = "Wanktube";		/* Service description */
Wanktube.SearchDesc    = "Wanktube";		/* Service search screen description */
Wanktube.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
Wanktube.CheckURL      = Wanktube_CheckURL;	/* CheckURL function */
Wanktube.GetURL        = Wanktube_GetURL;	/* GetURL function */
Wanktube.Search        = Wanktube_Search;	/* Search function */

// add Wanktube to site list
SiteList.push( Wanktube );
